#!/usr/bin/env groovy

pipeline {

    agent any
    // 全局变量
    environment {
        // 项目名称
        NAME = 'oops-server-manager'
        // 目标服务器用户名
        SERVER_USERNAME = 'root'
        // 目标服务器地址
        SERVER_ADDRESS = '172.31.90.45'
        // systemd service
        SYSTEMD_SERVICE_TEMPLATE = "[Unit]\\nDescription=${NAME} Service\\nAfter=network.target\\n\\n[Service]\\nExecStart=/usr/bin/java -jar /soft/${NAME}-1.0-SNAPSHOT/${NAME}-1.0-SNAPSHOT.jar\\nWorkingDirectory=/soft/${NAME}-1.0-SNAPSHOT/\\nRestart=always\\n\\n[Install]\\nWantedBy=multi-user.target\\n"
    }


    stages {
        stage('Clean workspace') {
            steps {
                /* Running on a fresh Docker instance makes this redundant, but just in
                 * case the host isn't configured to give us a new Docker image for every
                 * build, make sure we clean things before we do anything
                 */
                deleteDir()
                sh 'ls -lah'
            }
        }

        stage('Checkout') {
            steps {
                echo 'Checking...'
                checkout scm
                echo 'Checkout successful'
            }
        }

        stage('Build') {
            steps {
                echo 'Building'
                sh "mvn clean package"
                echo 'Build  successful'
            }
        }

        stage('Test') {
            steps {
                echo 'Testing'
                echo 'Test successful'
            }
        }

        /**
         * 生成配置文件暂时有问题
         */
//        stage('Generator') {
//            steps {
//                echo 'Generating'
//                sh "echo -e '${SYSTEMD_SERVICE_TEMPLATE}' /etc/systemd/system/111${NAME}.test"
//                echo 'Generator successful'
//            }
//        }

        /**
         * ssh无法登录,提示Pseudo-terminal will not be allocated because stdi
         * 写登陆脚本ssh的时候加个参数
         * ssh -t -t user@10.242.1.1
         * 或者
         * ssh user@10.242.1.1 -tt
         */
        stage('Deploy') {
            steps {
                echo 'Deploying...'
                sh "scp ${NAME}/target/${NAME}-1.0-SNAPSHOT-distribution.tar.gz ${SERVER_USERNAME}@${SERVER_ADDRESS}:~/downloads/"
                sh "ssh ${SERVER_USERNAME}@${SERVER_ADDRESS} -tt 'tar -xf /root/downloads/${NAME}-1.0-SNAPSHOT-distribution.tar.gz -C /soft/'"
                sh "scp ${NAME}/src/main/scripts/${NAME}.service ${SERVER_USERNAME}@${SERVER_ADDRESS}:/etc/systemd/system/"
                sh "ssh ${SERVER_USERNAME}@${SERVER_ADDRESS} -tt 'systemctl daemon-reload'"
                sh "ssh ${SERVER_USERNAME}@${SERVER_ADDRESS} -tt 'systemctl stop ${NAME}.service'"
                sh "ssh ${SERVER_USERNAME}@${SERVER_ADDRESS} -tt 'systemctl enable ${NAME}.service'"
                sh "ssh ${SERVER_USERNAME}@${SERVER_ADDRESS} -tt 'systemctl start ${NAME}.service'"
                echo 'Deploy successful'
            }
        }
    }
//    post {
//        always {
//            junit '**/target/*.xml'
//        }
//        failure {
//            mail to: 'team@example.com', subject: 'The Pipeline failed :('
//        }
//    }
}